package com.elong.android.specialhouse.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.elong.android.specialhouse.AppConstants;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.base.BaseVolleyActivity;
import com.elong.android.specialhouse.entity.HouseImageItem;
import com.elong.framework.netmid.response.IResponse;

import java.util.List;

public class ApartmentDetailImageBrowseActivity extends
		BaseVolleyActivity<IResponse<?>> {
	private FrameLayout flRoot;
	private ViewPager viewPager;
	private TextView tvPageNum;
	private TextView tvPicDesc;
	private List<HouseImageItem> mImageList;
	private ImageBrowseAdapter mImageViewAdapter;

	@Override
	protected void initContentView() {
		setContentView(R.layout.act_apartment_detail_image_browse);

		mImageList = (List<HouseImageItem>) getIntent().getSerializableExtra(
				AppConstants.ACTIVITY_KEY_HOUSE_IMAGES);
		flRoot = (FrameLayout) findViewById(R.id.fl_root);
		viewPager = (ViewPager) findViewById(R.id.view_pager);
		tvPageNum = (TextView) findViewById(R.id.tv_page_num);
		tvPicDesc = (TextView) findViewById(R.id.tv_pic_desc);

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initData();
	}

	private void initData() {
		mImageList = (List<HouseImageItem>) getIntent().getSerializableExtra(
				AppConstants.ACTIVITY_KEY_HOUSE_IMAGES);
		if (mImageList == null) {
			return;
		}
		mImageViewAdapter = new ImageBrowseAdapter(this, mImageList);
		viewPager.setAdapter(mImageViewAdapter);

		// 显示当前页码和总页数
		tvPageNum.setText(1 + "/" + mImageList.size());
		tvPicDesc.setText(mImageList.get(0).ImageNameCn);
		setListener();
	}

	private void setListener() {
		flRoot.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				back();
			}
		});

		viewPager.setOnPageChangeListener(new SimpleOnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				super.onPageSelected(position);
				tvPageNum.setText((position + 1) + "/" + mImageList.size());
				// TODO: 2015/8/27 这里应该是图片描述 ，暂时写的是图片名称
				tvPicDesc.setText(mImageList.get(position).ImageNameCn);
			}

		});
	}

	// private Handler mHandler = new Handler() {
	// 只在加载完第一张图片后才重新设置viewPager的宽高
	// private boolean shoudRelayout = false;
	// private ImageLoader imageLoader = ImageLoader.getInstance();
	// private DisplayImageOptions options;
	// private List<ApartmentDetailImageItem> mImageItems;
	// private LinearLayout mViewPagerParent;
	// private int mWidth;
	// private int mHeight;
	// public void handleMessage(android.os.Message msg) {
	// switch (msg.what) {
	// case 0:
	// if (shoudRelayout) {
	// FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(mWidth,
	// mHeight);
	// params.gravity = Gravity.CENTER;
	// mViewPagerParent.setLayoutParams(params);
	// }
	// break;
	//
	// default:
	// break;
	// }
	// }
	//
	// ;
	// };

	// private class ImageLoadlistener extends SimpleImageLoadingListener {
	// @Override
	// public void onLoadingComplete(Bitmap loadedImage) {
	// super.onLoadingComplete(loadedImage);
	// if (!shoudRelayout) {
	// mWidth = loadedImage.getWidth();
	// mHeight = loadedImage.getHeight();
	// // mHandler.sendEmptyMessage(0);
	// shoudRelayout = true;
	// }
	// }
	// }

}
